﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Sql;
using CourseSystem.Core.Entities;

namespace CourseSystem.Core.SqlServerDAL
{
    public class StudentDAL : CommonDal<StudentInfo>
    {
        public StudentDAL()
            : base(Settings.ConnectionName)
        {
        }

        public StudentDAL(string mConnectionStringName)
            : base(mConnectionStringName)
        {
        }

        public bool Review(string ids, int type)
        {
            if (string.IsNullOrEmpty(ids))
                return false;
            try
            {
                using (var dc = DataContext<StudentInfo>.CreateContext(ConnectionStringName))
                {
                    return dc.ExecuteCommand("update Student set RegType={0} where Id in (" + ids + ")", type) != 0;
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return false;
            }
        }

        public bool UpdateLastUrl(int id, string url)
        {
            using (var dc = DataContext<StudentInfo>.CreateContext(ConnectionStringName))
            {
                return dc.ExecuteCommand("update student set LastUrl={0} where id={1} and IsCanUpLastUrl=1", url, id) > 0;
            }
        }

        public bool LogoutSet(int id)
        {
            using (var dc = DataContext<StudentInfo>.CreateContext(ConnectionStringName))
            {
                return dc.ExecuteCommand("update student set IsCanUpLastUrl=0 where id={0}", id) > 0;
            }
        }

        public StudentInfo GetLastStu(int year, int subjectId)
        {
            var dc = DataContext<StudentInfo>.CreateContext(ConnectionStringName);
            var query = from item in dc.SourceTable where item.GradeYear == year && item.ClassInfo.SubjectId == subjectId select item;
            return query.OrderByDescending(o => o.Id).FirstOrDefault();
        }
    }
}
